package androidApplication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigInteger;
//import java.io.Path;
//import java.nio.file.Files;//user javase7, not supported by android
//import java.nio.file.Path;
//import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;

import android.os.Environment;
import android.util.Log;

import fileService.CSFileWrapper;

public class FileIO implements Serializable{
	private final String shareDir = "/Capstone/Share/";
	private final String tempDir = "/Capstone/Temp/";
	private final String configDir = "/Capstone/.config/";
	private File externalStorageDir;
	
	public FileIO(){
		Log.i("FileIO","Constructor");
		externalStorageDir = Environment.getExternalStorageDirectory();
		if(isMounted()){
			if(isWritable()){
				createFolder();
			}
		}
	}
	
	public ArrayList<String> getFileNames(){
		Log.i("FileIO","getFileNames()");
		ArrayList<String> fileNames = new ArrayList<String>();
		File share = new File(externalStorageDir + shareDir);
		
		if(share.listFiles() != null){
			for(File fileEntry : share.listFiles()){				
				fileNames.add(fileEntry.getName());
			}
		}
		
		return fileNames;
	}
	public CSFileWrapper getFile(String fileName){
		Log.i("FileIO","getFile()");
		CSFileWrapper file = null;
		
		try {
			File temp = new File(externalStorageDir + shareDir + fileName);
			FileInputStream fis = new FileInputStream(externalStorageDir + shareDir + fileName);
			byte[] data = new byte[(int) temp.length()];
			fis.read(data);
			file = new CSFileWrapper(data, fileName);
			fis.close();
		} catch (FileNotFoundException e) {e.printStackTrace();
		} catch (IOException e) {e.printStackTrace();
		}
		
		return file;
	}
	
	public  void writeFile(String fileName, byte[] data){
		Log.i("FileIO","writeFile()");
		
		try {
			Log.i("FileIO", String.valueOf(data.length));
			FileOutputStream fos = new FileOutputStream(externalStorageDir + shareDir + fileName);
			fos.write(data);
			fos.close();
		} catch (FileNotFoundException e) {e.printStackTrace();
		} catch (IOException e) {e.printStackTrace();
		}//
	}
	public void writePreviewFile(String fileName, byte[] data){
		Log.i("FileIO", "writePreviewFile()");
		
		try{
			FileOutputStream fos = new FileOutputStream(externalStorageDir + tempDir + fileName);
			fos.write(data);
			fos.close();
		}catch(FileNotFoundException e){e.printStackTrace();
		}catch(IOException e){e.printStackTrace();
		}
	}
	public void cleanTempFolder(){
		File file = new File(externalStorageDir + tempDir);
		if(file.isDirectory()){
			String[] myFiles = file.list();
			for(String s : myFiles){
				File myFile = new File(file, s);
				myFile.delete();
			}
		}
	}
	
	public void createFolder(){
		Log.i("FileIO","createFolder()");
		File dir = new File(externalStorageDir + shareDir);
		
		if(!dir.exists()){
			dir.mkdirs();
		}
		dir = new File(externalStorageDir + tempDir);
		if(!dir.exists()){
			dir.mkdir();
		}
		
		dir = new File(externalStorageDir + configDir);
		if(!dir.exists()){
			dir.mkdir();
			genConfigFile();
		}
	}
	
	public boolean isWritable(){
		Log.i("FileIO","isWrtiable()");
		String state = Environment.getExternalStorageState();
		
		if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
			return false;
		}
		
		return true;
	}
	public File getExternalStorageDir() {
		return externalStorageDir;
	}

	public void setExternalStorageDir(File externalStorageDir) {
		this.externalStorageDir = externalStorageDir;
	}

	public String getShareDir() {
		return shareDir;
	}

	public String getTempDir() {
		return tempDir;
	}

	public boolean isMounted(){
		Log.i("FileIO","isMounted()");
		String state = Environment.getExternalStorageState();
		
		if(Environment.MEDIA_MOUNTED.equals(state)){
			return true;
		}
		
		return false;
	}
	public ArrayList<String> getConfigFile(){
		File file = new File(externalStorageDir + configDir + "aaconfig.txt");
		ArrayList<String> config = new ArrayList<String>();
		
		try{
			BufferedReader in = new BufferedReader(new FileReader(file));
			String configLine = "";
			while((configLine = in.readLine()) != null){
				config.add(configLine);
			}
			in.close();
		}catch(FileNotFoundException e){e.printStackTrace();
		}catch(IOException e){e.printStackTrace();
		}
		
		return config;
	}
	public void updateConfigFile(ArrayList<String> config){
		File conFile = new File(externalStorageDir + configDir + "aaconfig.txt");
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter(conFile, false));
			for(String s : config){
				out.write(s);
				out.newLine();
			}
			out.close();
		}catch(IOException ex){ex.printStackTrace();
		}
	}
	private void genConfigFile(){
		File conFile = new File(externalStorageDir + configDir + "aaconfig.txt");
		if(!conFile.exists()){			
			String initData = "";
			//RSAKey initial
			initData += "rsaexp:";			
			Random rand = new Random();
			BigInteger exp = BigInteger.valueOf((rand.nextInt(89999)+10000));			
			initData += exp;
			
			//		
			try {
				conFile.createNewFile();
				
				BufferedWriter out = new BufferedWriter(new FileWriter(conFile, false));
				out.write(initData);
				out.newLine();
				out.close();
			} catch (IOException e) {e.printStackTrace();
			}
		}
	}
}
